/**
 * 
 */
package fr.flogo.destinee.pads;

import java.util.Timer;

import fr.flogo.destinee.cameras.ICamera;
import android.opengl.GLSurfaceView;
import android.util.TimeUtils;
import android.view.MotionEvent;

/**
 * @author golestif
 *
 */
public class FullScreen4dPad {
	private static final long REACTIVITY = 25;

	static private FullScreen4dPad instance = null;
	
	private GLSurfaceView surface = null;

	private ICamera camera;
	
	private Timer timer;

	private long lastDownTime = 0;
	
	private final float piover180 = 0.0174532925f;
	private float heading = 0.0f;
	
	public FullScreen4dPad(GLSurfaceView surface, ICamera camera) {
		this.surface = surface;
		this.camera = camera;
		this.timer = new Timer();
		
		if (FullScreen4dPad.instance == null) {
			FullScreen4dPad.instance = this;
		}
	}
	
	public void onTouch(final MotionEvent event) {
		if (event.getEventTime() < (lastDownTime + FullScreen4dPad.REACTIVITY)) {
			return ;
		}
		this.lastDownTime = event.getEventTime();
		
		float halfScreen = this.surface.getWidth() / 4;
		float halfScreenY = this.surface.getHeight() / 4;

		
		
		if (event.getX() > 3 * halfScreen) { // droite
			System.out.println("position droite");
//			camera.turn(1f, 0.0f, 1.0f, 0.0f);
			camera.move(0.2f, 0.0f, 0f);

//			heading += 1000f;
		} else if (event.getX() < halfScreen) { // gauche
			System.out.println("position gauche");
//			camera.turn(-1f, 0.0f, 1f, 0.0f);
			camera.move(-0.2f, 0.0f, 0f);

//			heading -= 1000f;
		}

		if (event.getY() > 3 * halfScreenY) {
			System.out.println("position haut");
			camera.move(0.0f, 0.0f, 0.2f);
			
//			float pos[] = camera.getPosition();
//			System.out.println("Premiere position : ["+pos[0]+", "+pos[2]+"]");
//			pos[0] -= (float)Math.sin(heading * piover180) * 0.0001f;	//Move On The X-Plane Based On Player Direction
//			pos[2] -= (float)Math.cos(heading * piover180) * 0.0001f;	//Move On The Z-Plane Based On Player Directio
//			System.out.println("Nouvelle position : ["+pos[0]+", "+pos[2]+"]");
//			camera.move(pos[0], 0.0f, pos[2]);
			
		} else if (event.getY() < halfScreenY) {
			System.out.println("position bas");
			camera.move(0.0f, 0.0f, -0.2f);
			
//			float pos[] = camera.getPosition();
//			pos[0] += (float)Math.sin(heading * piover180) * 0.0001f;	//Move On The X-Plane Based On Player Direction
//			pos[2] += (float)Math.cos(heading * piover180) * 0.0001f;	//Move On The Z-Plane Based On Player Directio
//			camera.move(pos[0], 0.0f, pos[2]);
		}
	}
	
	static public FullScreen4dPad getInstance() {
		return FullScreen4dPad.instance;
	}
}
